package pers.linkong.asr.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pers.linkong.asr.domain.CourseBean;
import pers.linkong.asr.domain.CourseSublevelBean;
import pers.linkong.asr.mapper.CourseMapper;
import pers.linkong.asr.util.TimeKit;

@Service
// Service组件
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, readOnly = false)
public class CourseService {
	@Autowired
	private CourseMapper courseMapper;

	// 增
	public void addCourse(CourseBean course) throws Exception {
		course.setCreatedate(TimeKit.getNowDate());
		course.setStartdate(TimeKit.YMDFormatTS(course.getStartdateStr()));
		this.courseMapper.addCourse(course);
		System.out.println("添加课程：" + course.getCid());
		// throw new RuntimeException();
	}

	// 删
	public void deleteCourse(int Cid) throws Exception {
		this.courseMapper.deleteCourse(Cid);
		System.out.println("删除课程：" + Cid);
	}

	// 查
	public CourseBean getCourseById(int id) throws Exception {
		CourseBean course = courseMapper.getCourseById(id);
		System.out.println("查询课程：" + course.getCname());
		return course;
	}

	// 改
	public void updateCourse(CourseBean course) throws Exception {
		course.setStartdate(TimeKit.YMDFormatTS(course.getStartdateStr()));
		this.courseMapper.updateCourse(course);
		System.out.println("修改课程：" + course.getCname());
	}

	// 分页
	public List<CourseBean> findCourseByPage(CourseBean course, long start,
			long limit) throws Exception {
		List<CourseBean> courseBeans = this.courseMapper.findCourseByPage(course, start, limit);
		return courseBeans;
	}
	
	// 统计
	public long getCourseCount(CourseBean course) throws Exception {
		return this.courseMapper.getCourseCount(course);
	}

	// 查询当前班级拥有的课程数量
	public long getCourseCountBySclass(String sclass) {
		return this.courseMapper.getCourseCountBySclass(sclass);
	}
	
	// 查询复合要求的课程
	public List<CourseSublevelBean> getCourseBySclass(String sclass){
		return this.courseMapper.getCourseBySclass(sclass);
	}
	
	// 根据课程id获取当前第几周
	public int getNowWeekNum(int cid){
		return this.courseMapper.getNowWeekNum(cid);
	}
	
	// 根据班级名查询班级ID
	public String getClassidByName(String classname){
		return this.courseMapper.getClassidByName(classname);
	}
	
	// 根据教师id查询课程
	public List<CourseBean> getCourseByTid(int tid){
		return this.courseMapper.getCourseByTid(tid);
	}
}
